package com.tanhua.manage.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.tanhua.manage.pojo.Role;
import com.tanhua.manage.service.RoleService;
import com.tanhua.manage.util.NoAuthorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author Au
 * @DATE 2020/12/13 18:57
 **/
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 查询列表
     * @return
     */
    @GetMapping
    @NoAuthorization
    public ResponseEntity<List<Role>> queryRole() {

        List<Role> roles = roleService.queryRole();

        if (roles != null) {
            return ResponseEntity.ok(roles);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/query")
    @NoAuthorization
    public ResponseEntity<List<Role>> queryRoleByRoleName(@RequestParam(value = "roleName", required = false) String roleName) {

        List<Role> roles = roleService.queryRoleByRoleName(roleName);

        if (roles != null) {
            return ResponseEntity.ok(roles);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @NoAuthorization
    public ResponseEntity<Void> deleteRoleById(@PathVariable("id") Long id) {
        boolean flag = roleService.deleteRoleById(id);
        if (flag) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 保存角色
     * @param roleName
     * @param roleDesc
     * @param state
     * @param permissionIds
     * @return
     */
    @PostMapping("/save")
    @NoAuthorization
    public ResponseEntity<Void> saveRole(@RequestParam(value = "roleName", required = false) String roleName,
                                         @RequestParam(value = "roleDesc", required = false) String roleDesc,
                                         @RequestParam(value = "state", required = false) Integer state,
                                         @RequestParam(value = "permissionIds", required = false) Integer[] permissionIds) {
        boolean flag = roleService.saveRole(roleName, roleDesc, state, permissionIds);
        if (flag) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 更新角色
     * @param roleId
     * @param permissionIds
     * @return
     */
    @PutMapping("/update")
    @NoAuthorization
    public ResponseEntity<Void> updateRole(@RequestParam(value = "roleId", required = false) Long roleId,
                                           @RequestParam(value = "permissionIds", required = false) Integer[] permissionIds) {
        boolean flag = roleService.updateRole(roleId, permissionIds);
        if (flag) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 更新状态
     * @param roleId
     * @param state
     * @return
     */
    @PutMapping("/state")
    @NoAuthorization
    public ResponseEntity<Void> updateState(@RequestParam(value = "roleId", required = false) Long roleId,
                                            @RequestParam(value = "state", required = false) Integer state) {
        boolean flag = roleService.updateState(roleId, state);
        if (flag) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }



}
